/**
   Copyright 2012 Shakil Siraj (shakil.siraj@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.googlecode.objectify.test.dataloader.example.bookstore.dao;

import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.test.dataloader.example.bookstore.Author;
import com.googlecode.objectify.test.dataloader.example.bookstore.Book;
import com.googlecode.objectify.test.dataloader.example.bookstore.Publisher;

/**
 * @author Shakil Siraj (shakil.siraj@gmail.com)
 * 
 */
public class BooksDAO {

	private final Objectify ofy = ObjectifyService.ofy();

	public Integer getTotalNumberOfBooks() {
		return ofy.load().type(Book.class).keys().list().size();
	}

	public List<Book> searchBooksByIsbn(String isbn) {
		return ofy.load().type(Book.class).filter("isbn >=", isbn)
				.filter("isbn <", isbn + "\uFFFD").list();
	}

	public List<Book> searchBooksByTitle(String title) {
		return ofy.load().type(Book.class).filter("title >=", title)
				.filter("title <", title + "\uFFFD").list();
	}

	public List<Book> searchBooksByStartingCharacter(String firstCharacter) {
		String c = firstCharacter.substring(0, 1);
		return ofy.load().type(Book.class).filter("title >= ", c)
				.filter("title < ", c + "\uFFFD").list();
	}

	public Integer getNumberOfBooksStartingWithCharacter(String firstCharacter) {
		String c = firstCharacter.substring(0, 1);
		return ofy.load().type(Book.class).filter("title >= ", c)
				.filter("title < ", c + "\uFFFD").keys().list().size();
	}

	public List<Book> searchBooksByPublisher(Key<Publisher> publisherKey) {
		return ofy.load().type(Book.class).filter("publisher", publisherKey)
				.list();
	}
	
	public List<Book> searchBooksByPublisher(Ref<Publisher> publisherRef){
		return ofy.load().type(Book.class).filter("publisher", publisherRef)
				.list();
	}

	public List<Book> searchBooksByAuthorFirstName(String authorFirstName) {
		return searchBooksForAuthors(ofy.load().type(Author.class)
				.filter("firstName >=", authorFirstName)
				.filter("firstName < ", authorFirstName + "\uFFFD").keys()
				.list());
	}

	public List<Book> searchBooksByAuthorLastName(String authorLastName) {
		return searchBooksForAuthors(ofy.load().type(Author.class)
				.filter("lastName >=", authorLastName)
				.filter("lastName < ", authorLastName + "\uFFFD").keys().list());
	}

	public List<Book> searchBooksForAuthors(List<Key<Author>> authorsList) {
		return ofy.load().type(Book.class).filter("authors in ", authorsList)
				.list();
	}

	public List<Book> searchBooksByPublisher(String publisherName) {
		return searchBooksByPublisher(ofy.load().type(Publisher.class)
				.filter("name", publisherName).keys().first().now());
	}
}
